<?php
class Job_model extends CI_Model {

	public function __construct()
	{
		$this->load->database();
	}
    public function verify_job($job_number, $postcode)
    {
        $query = $this->db->query(' SELECT * FROM address, address_has_customer
                                    WHERE address.postcode = "'.$postcode.'"
                                    AND address_has_customer.customer_idcustomer IN (
                                    SELECT customer_idcustomer 
                                    FROM job 
                                    WHERE job.idjob = '.$job_number.')');
        
        return $query->result();
    }
    public function get_updates($job_number){
        $query = $this->db->query(' SELECT descr, time, type, name, employee_idemployee AS "id"
                                    FROM jobupdate, employee
                                    WHERE job_idjob ='.$job_number.' 
                                    AND jobupdate.employee_idemployee = employee.idemployee
                                    ORDER BY idjobupdate desc');

        return $query -> result();
    }
    
    public function job_open($job_number){
        $query = $this->db->query(' SELECT * 
                                    FROM job 
                                    WHERE idjob = '.$job_number.' and open = 1');
                                    
        return $query->result();
    }
    public function add_comment($comment, $job_number, $employee){
        $string = ' INSERT INTO jobupdate (descr, job_idjob, employee_idemployee)  
                    VALUES ("'.$comment.'",'.$job_number.', '.$employee.');';

        if ($query = $this->db->query($string)){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    public function get_customer_details($idcustomer){
        $query = $this->db->query('');
        return $this->session->userdata('job_number');
    }
    public function get_job_data($job_number){
        $query = $this->db->query('     SELECT job.descr as "job_descr", open, time, jobtype_idjobtype, name, active, jobtype.descr as "jobtype_descr"
                                        FROM job,employee, jobtype 
                                        WHERE idjob = '.$job_number.' 
                                        AND job.employee_idemployee = employee.idemployee
                                        AND jobtype.idjobtype = job.jobtype_idjobtype');
        $row = $query->row();
        return $row;
    }
}